package com.liu.nb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.dto.GetPoleByCircuitryDto;
import com.liu.nb.entity.Circuitry;
import com.liu.nb.mapper.CircuitryMapper;
import com.liu.nb.mapper.CircuitryPoleMapper;
import com.liu.nb.service.ICircuitryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 线路表 服务实现类
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-31
 */
@Service
public class CircuitryServiceImpl extends ServiceImpl<CircuitryMapper, Circuitry> implements ICircuitryService {
    @Autowired
    private CircuitryMapper circuitryMapper;
    @Resource
    private CircuitryPoleMapper circuitryPoleMapper;
    @Override
    public Circuitry selCircuitry(String id) {
        return circuitryMapper.selectById(id);
    }

    @Override
    public List<GetPoleByCircuitryDto> getPoleCircuityDto() {
        List<Circuitry> list = this.list(new QueryWrapper<Circuitry>().select("id","name"));
        List<GetPoleByCircuitryDto> dtoList=new ArrayList<>();
        list.forEach(i->{
            GetPoleByCircuitryDto dto = new GetPoleByCircuitryDto();
            dto.setId(i.getId());
            dto.setName(i.getName());
            dtoList.add(dto);
        });
        return dtoList;
    }

    @Override
    public IPage<Circuitry> getAll(Integer pageNo,Integer pageSize, Integer id, Integer state) {
        Page<Circuitry> page=new Page<>(pageNo,pageSize);
        return circuitryMapper.getAll(page,id,state);
    }

    @Override
    public List<Circuitry> getCircuitryByPole(String poleId) {
        QueryWrapper<Circuitry> queryWrapper=new QueryWrapper<>();
        queryWrapper.inSql("id","SELECT circuity_id FROM circuitry_pole WHERE pole_id = '"+poleId+"'");
        List<Circuitry> list = this.list(queryWrapper);
        return list;
    }
}
